package com.yqxc.folkspace.util;

import org.fisco.bcos.sdk.crypto.CryptoSuite;
import org.fisco.bcos.sdk.crypto.keypair.CryptoKeyPair;
import org.fisco.bcos.sdk.crypto.signature.Signature;
import org.fisco.bcos.sdk.crypto.signature.SignatureResult;
import org.fisco.bcos.sdk.transaction.codec.encode.TransactionEncoderService;
import org.fisco.bcos.sdk.transaction.signer.TransactionSignerFactory;
import org.fisco.bcos.sdk.transaction.signer.TransactionSignerInterface;

/**
 * @author 元启星辰（北京）科技有限公司
 * @describe 交易签名服务层
 * @date 2021-12-25
 */
public class MyTransactionEncoderUtil extends TransactionEncoderService {

    // 签名模块
    private final Signature signature;
    // 交易签名模块
    private final TransactionSignerInterface transactionSignerService;


    /**
     * 构造函数，初始化服务
     *
     * @param cryptoSuite CryptoSuite密码学实例
     */
    public MyTransactionEncoderUtil(CryptoSuite cryptoSuite) {

        super(cryptoSuite);
        this.signature = cryptoSuite.getSignatureImpl();
        this.transactionSignerService = TransactionSignerFactory.createTransactionSigner(signature);
    }

    /**
     * 签名
     *
     * @param hash          需要签名的字节数组
     * @param cryptoKeyPair 密钥对
     * @return 签名结果SignatureResult
     */
    public SignatureResult sign(byte[] hash, CryptoKeyPair cryptoKeyPair) {

        return transactionSignerService.sign(hash, cryptoKeyPair);
    }

}
